package com.example.administrator.x2picturetransliteration.Home.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.Base.BaseActivity;
import com.example.administrator.x2picturetransliteration.Base.BasePresenter;
import com.example.administrator.x2picturetransliteration.Constants;
import com.example.administrator.x2picturetransliteration.Home.Dialog.AgreementDialog;
import com.example.administrator.x2picturetransliteration.Home.Presenter.Activity.SignInPresenter;
import com.example.administrator.x2picturetransliteration.R;
import com.example.administrator.x2picturetransliteration.Util.SpUtils;
import com.example.administrator.x2picturetransliteration.Util.ToastUtil;
import com.example.administrator.x2picturetransliteration.Util.UmengUtil;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登录
 * Created by Administrator on 2019/8/16.
 */

public class SignInActivity extends BaseActivity{

    public static final int QITADLU = 0;//其他登录
    public static final int WEIXING = 1;//微信登录
    public static final int QQDENGL = 2;//QQ登录

    @BindView(R.id.AImg)ImageView AImg; //用户协议按钮

    @BindView(R.id.gsText)TextView gsText;

    private SignInPresenter mSignInPresenter;

    private UmengUtil mUmengUtil;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_sign_in;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String gsName = Constants.getConstants().getmAppDataBean().getCompanyName();

        if (gsName!=null)gsText.setText(gsName);

        mSignInPresenter = new SignInPresenter(this,mCompositeSubscriptions);

        mUmengUtil = new UmengUtil();

        if (!"notoken".equals(getIntent().getStringExtra("notoken")))mSignInPresenter.userDao();

        showCoverDialog();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK){

            switch (requestCode){

                case QITADLU: //处理其他登录界面返回的数据

                    Map<String,String> map = new HashMap<>();
                    map.put("mobile",data.getStringExtra("Telephone"));
                    map.put("password",data.getStringExtra("Pwd"));
                    //map.put("channelCode", Constants.getConstants().getChannelCode());
                    mSignInPresenter.login(map);
                    break;

                case WEIXING:

                    mSignInPresenter.updateUserDao(Constants.getConstants().getToken(),"","");
                    break;
            }

        }

    }

    /** 注册 */
    @OnClick(R.id.RegisterView)
    public void RegisterViewClick(){
        if(!SpUtils.getInstance().getAgree(this)){
            showCoverDialog();
            return;
        }
        startActivity(new Intent(this,RegisterActivity.class));

    }

    /** 微信登录 */
    @OnClick(R.id.WxSignInView)
    public void WxSignInViewClick(){
        if(!SpUtils.getInstance().getAgree(this)){
            showCoverDialog();
            return;
        }
        if (!AJudge){

            new ToastUtil(this).getToast(false,"请同意用户协议").show();
            return;
        }

        mUmengUtil.umengDeleteOauth(this, SHARE_MEDIA.WEIXIN);
        mUmengUtil.shareLoginUmeng(this, SHARE_MEDIA.WEIXIN, new UmengUtil.LonginUmeng() {
            @Override
            public void isData(final String uid, final String name, final String iconurl) {

                if (uid==null){

                    //new ToastUtil(SignInActivity.this).getToast(false,"授权失败").show();
                    return;
                }

                Map<String,String> map = new HashMap<>();
                map.put("openid",uid);
                map.put("nickname",name);
                map.put("headPic",iconurl);
                //map.put("channelCode",Constants.getConstants().getChannelCode());

                mSignInPresenter.wxlogin(map, new BasePresenter.Callback() {
                    @Override
                    public void getData(Object data) {

                        Map<String,String> httpdata = (Map<String,String>)data;

//                        if ("0".equals(httpdata.get("bind"))){
//
//                            //Constants.getConstants().setToken(httpdata.get("token"));
//                            ForgetActivity.startForgetActivity(SignInActivity.this,"",uid,name,iconurl,"1");
//
//                        }else {
//
//                            mSignInPresenter.updateUserDao(httpdata.get("token"),"","");
//                        }

                        mSignInPresenter.updateUserDao(httpdata.get("token"),"","");
                    }
                });

            }
        });

    }

    /** QQ登录 */
    @OnClick(R.id.QQSignInView)
    public void QQSignInViewClick(){
        if(!SpUtils.getInstance().getAgree(this)){
            showCoverDialog();
            return;
        }

        if (!AJudge){

            new ToastUtil(this).getToast(false,"请同意用户协议").show();
            return;
        }

        mUmengUtil.umengDeleteOauth(this, SHARE_MEDIA.QQ);
        mUmengUtil.shareLoginUmeng(this, SHARE_MEDIA.QQ, new UmengUtil.LonginUmeng() {
            @Override
            public void isData(final String uid, final String name, final String iconurl) {

                if (uid==null){

                    //new ToastUtil(SignInActivity.this).getToast(false,"授权失败").show();
                    return;
                }

                Map<String,String> map = new HashMap<>();
                map.put("openid",uid);
                map.put("nickname",name);
                map.put("headPic",iconurl);
                //map.put("channelCode",Constants.getConstants().getChannelCode());

                mSignInPresenter.qqlogin(map, new BasePresenter.Callback() {
                    @Override
                    public void getData(Object data) {

                        Map<String,String> httpdata = (Map<String,String>)data;

//                        if ("0".equals(httpdata.get("bind"))){
//
//                            //Constants.getConstants().setToken(httpdata.get("token"));
//                            ForgetActivity.startForgetActivity(SignInActivity.this,"",uid,name,iconurl,"2");
//
//                        }else {
//
//                            mSignInPresenter.updateUserDao(httpdata.get("token"),"","");
//                        }

                        mSignInPresenter.updateUserDao(httpdata.get("token"),"","");
                    }
                });

            }
        });

    }

    /** 其他登录 */
    @OnClick(R.id.QtText)
    public void QtTextClick(){
        if(!SpUtils.getInstance().getAgree(this)){
            showCoverDialog();
            return;
        }
        startActivityForResult(new Intent(this,OtherActivity.class),QITADLU);
    }

    /** 同意用户协议 */
    private boolean AJudge = false;
    @OnClick(R.id.A)
    public void AClick(){

        if (AJudge){

            AImg.setImageResource(R.mipmap.home_check0);

        }else {

            AImg.setImageResource(R.mipmap.home_check1);
        }

        AJudge = !AJudge;
    }

    /** 用户协议 */
    @OnClick(R.id.B)
    public void BClick(){

        Agreement("1");
    }

    /** 隐私政策 */
    @OnClick(R.id.C)
    public void CClick(){

        Agreement("2");
    }

    private void showCoverDialog() {

        mSignInPresenter.Agreement("2",new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                new AgreementDialog(SignInActivity.this, new AgreementDialog.AgreementHd() {
                    @Override
                    public void hdff() {
                        agree();
                    }

                    @Override
                    public void sbhlb(String sb) {

                        Agreement(sb);
                    }

                    @Override
                    public void onRefuse() {
                        SpUtils.getInstance().agree(SignInActivity.this,false);
                    }
                }).show();
            }
        });

    }

    private void agree(){
        SpUtils.getInstance().agree(this,true);
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "");
    }

    public void Agreement(final String sb){
        mSignInPresenter.Agreement(sb,new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {
                AgreementActivity.startAgreementActivity(SignInActivity.this,"1".equals(sb)?"服务协议":"隐私政策",data + "");
            }
        });
    }
}
